﻿using System.Collections;
using System.Collections.Generic;
using DG.DemiLib;
using UnityEngine;

public class BlendShapeCtrl : MonoBehaviour
{
    SkinnedMeshRenderer skinnedMeshRenderer;
    Mesh skinnedMesh;

    public enum MotherName
    {
        One = 0,
        Two
    }

    [Header("母亲BlendShape影响列表")] public List<string> motherBlendShapes;
    [Space] 
    [Space]
    [Header("父亲的参数配置")] public List<GameObject> fatherNodes;
    private List<Vector3> fatherPositions = new List<Vector3>();
    private bool isReset = false;

    [Header("母亲的参数配置")]
    [Header("母亲BlendShape名列表")] 
    public List<string> motherNames;
    [Header("母亲对应位置配置列表")] 
    public List<Vector3> motherPositions;
    [Header("母亲BlendShape控制列表")] 
    public List<float> motherValues;
    [Header("调节的母亲序号")] 
    public MotherName motherSelect;

    void Awake()
    {
        Debug.Log("OnAwake");
        skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
        skinnedMesh = GetComponent<SkinnedMeshRenderer>().sharedMesh;
        ResetFatherPosition();
    }

    public void OnInit()
    {
        Debug.Log("OnInit");
        skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
        skinnedMesh = GetComponent<SkinnedMeshRenderer>().sharedMesh;
        ResetFatherPosition();
    }

    public void ResetFatherPosition()
    {
        fatherPositions.Clear();
        foreach (GameObject obj in fatherNodes)
            fatherPositions.Add(new Vector3(
                obj.transform.localPosition.x, 
                obj.transform.localPosition.y, 
                obj.transform.localPosition.z));
    }

    public void ResetBlendShapeNodePosition()
    {
        Debug.Log("ResetBlendShapeNodePosition ------------------");
        isReset = true;

        for (int i = 0; i < motherValues.Count; i++)
            motherValues[i] = 0;
        
        for (int i = 0; i < fatherNodes.Count; i++)
        {
            Vector3 vec = fatherPositions[i];
            Debug.Log(string.Format("Reset {0}, pos = {1}", fatherNodes[i].name, vec.z));
            fatherNodes[i].transform.localPosition = new Vector3(vec.x, vec.y, vec.z);
        }

        isReset = false;
    }

    public void OnBlendShapeCtrlChanged()
    {
        if (isReset) return;
        int index = (int) motherSelect;
        SetBlendShapeNodesPosition(motherNames[index], motherValues[index], index, true);
    }

    public void SetBlendShapeNodesPosition(string name, float value, int index, bool needSync = false)
    {
        if (isReset) return;
        if (name != null)
        {
            //保证在0~100区间
            value = value > 100 ? 100 : (value < 0 ? 0 : value);
            
            //先修改BlendShape的值
            if (needSync)
                SetBlendShapeValueByName(name, value);

            float rate = value/100;
            int count = fatherNodes.Count;
            int mindex = 0;
            Vector3 vec0;
            Vector3 vec1;
            
            Debug.Log(string.Format("Set {0} Pos value = {1}, rate = {2}", name, value, rate));
            
            //计算需要调整的位置
            for (int i = 0; i < count; i++)
            {
                mindex = i + count * index;
                if (mindex < motherPositions.Count)
                {
                    vec0 = fatherPositions[i];
                    vec1 = motherPositions[mindex];
                    fatherNodes[i].transform.localPosition = vec0 + (vec1 - vec0) * rate;
                    //Debug.Log(string.Format("======>{0} 0 = {1}, 1 = {2}, 2 = {3}",
                    //    fatherNodes[i].name, vec0.z, vec1.z, fatherNodes[i].transform.localPosition.z));
                }
            }
        }
    }

    public void SetBlendShapeValueByName(string name, float weight)
    {
        var index = skinnedMesh.GetBlendShapeIndex(name);
        if (index > 0)
            skinnedMeshRenderer.SetBlendShapeWeight(index, weight);

        CheckBlendShapeNode(name, weight);
    }

    public void CheckBlendShapeNode(string name, float weight)
    {
        int index = -1;
        for (int i = 0; i < motherNames.Count; i++)
            if (motherNames[i] == name) index = i;

        if (index >= 0)
            SetBlendShapeNodesPosition(name, weight, index);
    }
    
    public void ResetMotherBlendShapes()
    {
        //重置所有的BlendShape值
        foreach (string name in motherBlendShapes) SetBlendShapeValueByName(name, 0);
        
        //重置相关骨骼节点数值
        ResetBlendShapeNodePosition();
    }
}
